package atom.core3.j2ee.privilege;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import atom.core2.j2ee.util.UrlUtil;
import atom.core3.j2ee.privilege.bean.LoginSessionInfo;
import atom.core3.j2ee.privilege.bean.SessionPrivilegeResource;

public class PrivilegeResourceManager
{
	public static LoginSessionInfo getLoginSessionInfo(SessionPrivilegeResource sessionPrivilegeResource,String uri)
	{
		LoginSessionInfo sessionInfo = new LoginSessionInfo();
		Map resMap = sessionPrivilegeResource.getPrivileges();
		sessionInfo.setLoginId(sessionPrivilegeResource.getLoginId());
		sessionInfo.setObjectBytes(sessionPrivilegeResource.getObjectBytes());
		sessionInfo.setSsokey(sessionPrivilegeResource.getSsokey());
		String limitId;
		if (resMap == null||uri==null) 
		{
			limitId="";
		}
		else
			limitId = PrivilegeResourceManager.getLimitId(resMap, uri);
		sessionInfo.setLimitId(limitId);
		return sessionInfo;
		
	}
	public static LoginSessionInfo getLoginSessionInfo(SessionPrivilegeResource sessionPrivilegeResource)
	{
		LoginSessionInfo sessionInfo = new LoginSessionInfo();
		Map resMap = sessionPrivilegeResource.getPrivileges();
		sessionInfo.setLoginId(sessionPrivilegeResource.getLoginId());
		sessionInfo.setObjectBytes(sessionPrivilegeResource.getObjectBytes());
		sessionInfo.setSsokey(sessionPrivilegeResource.getSsokey());
		sessionInfo.setLimitId("");
		return sessionInfo;
		
	}
	public static String getLimitId(Map resMap,String uri)
	{
		WebPrivilegeResource res = null;
		Iterator it=resMap.entrySet().iterator();
		while(it.hasNext()){
		    Map.Entry entry=(Map.Entry)it.next();
		    String key = ((String)entry.getKey()).trim();
		    int npos = key.indexOf("\r\n");
		    if (npos==-1)
		    {
		    	npos = key.indexOf("\n");
		    }
		    
		    if (npos>0)
		    {
		    	key = key.substring(0,npos-1);
		    }
		    String uriprefix = UrlUtil.getUrlPrefix(uri);
		    String keyprefix = UrlUtil.getUrlPrefix(key);
		    
		    //如果key是uri的子串，表明有权限 method=a 则method=aaa也通过了 这样不好
		    if (uriprefix.indexOf(keyprefix)>=0)
		    {
		    	//继续匹配method部分
			    String uriSuffix = UrlUtil.getUrlSuffix(uri);
			    String keySuffix = UrlUtil.getUrlSuffix(key);
		    	if (StringUtils.isEmpty(keySuffix))
		    	{
		    		res = (WebPrivilegeResource)entry.getValue();
		    		break;
		    	}
		    	else
		    	{
		    		if (keySuffix.equals(uriSuffix))
		    		{
		    			res = (WebPrivilegeResource)entry.getValue();
		    			break;
		    		}
		    		else
		    			continue;
		    	}
		    }
		    
		}		
		boolean hasPrivilege = res != null;
		
		// 有权限则取出权限路径.
		if (hasPrivilege) {
			// 当前动作.
			return res.getLimitId();
		}
		else {
			return "";//登录了但没有权限返回是""
		}
		
	}
	public static Map getLimitIds(Map resMap)
	{
		WebPrivilegeResource res = null;
		HashMap limitidMap = new HashMap();
		Iterator it=resMap.entrySet().iterator();
		while(it.hasNext())
		{
		    Map.Entry entry=(Map.Entry)it.next();
		    res = (WebPrivilegeResource)entry.getValue();
		    limitidMap.put(res.getLimitId(), res.getLimitId());
		}
		return limitidMap;
	}
	//privilegeLevel =1 代表一级，取getLimitId的长度为2
	//LimitId 一个级别长度是2
	//那获取菜单的应该传入2，那小于等于4的都是
	public static Map getLimitIds(Map resMap,int privilegeLevel)
	{
		WebPrivilegeResource res = null;
		HashMap limitidMap = new HashMap();
		Iterator it=resMap.entrySet().iterator();
		while(it.hasNext())
		{
		    Map.Entry entry=(Map.Entry)it.next();
		    res = (WebPrivilegeResource)entry.getValue();
		    if(res.getLimitId().length()<=privilegeLevel*2)
		    	limitidMap.put(res.getLimitId(), res.getLimitId());
		}
		return limitidMap;
	}
	public static Map getSonLimitIds(Map resMap,String parentLimitId)
	{
		//privilegeLevel =1 代表一级，取getLimitId的长度为2
		//LimitId 一个级别长度是2
		WebPrivilegeResource res = null;
		HashMap limitidMap = new HashMap();
		Iterator it=resMap.entrySet().iterator();
		while(it.hasNext())
		{
		    Map.Entry entry=(Map.Entry)it.next();
		    res = (WebPrivilegeResource)entry.getValue();
		    //长度必须相差一个等级
		    if (parentLimitId.length()!=res.getLimitId().length()- 2)
		    	continue;
		    
		    //即父是0202 那子应该是0202XX
		    if(res.getLimitId().substring(0, parentLimitId.length()).equals(parentLimitId))
		    {
		    	limitidMap.put(res.getLimitId(), res.getLimitId());
		    }
		}
		return limitidMap;
	}	

}
